package GUILayer;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import CtrLayer.CustomerCtr;
import CtrLayer.ProductCtr;
import CtrLayer.SalesOrderCtr;
import ModelLayer.Customer;
import ModelLayer.PartOrder;
import ModelLayer.Product;
import ModelLayer.SalesOrder;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.table.DefaultTableModel;


public class SalesOrderUI extends JPanel {
	private JTextField txtName;
	private JTextField txtAddress;
	private JTextField txtCodeZip;
	private JTextField txtCity;
	private JTextField txtPhoneNo;
	private JTextField txtEmail;
	private JTextField txtCprCvr;

	private JPanel panelErrorAddress;
	private JPanel panelErrorCodeZip;
	private JPanel panelErrorEmail;
	private JPanel panelErrorCustomerName;
	private JPanel panelErrorCity;
	private JPanel panelErrorCprCvr;
	private CustomerTableModel customerTableModel;
	private CustomerCtr customerCtr;
	private JPanel panelErrorPhoneNo;
	private JButton btnClear;
	private JTextField txtDate;
	private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
	private Date date = new Date();
	private JTextField textField;
	private final ButtonGroup buttonGroupPayment = new ButtonGroup();
	private JTextField txtFilter;
	private SalesProductTableModel prodTableModel;
	private JTextField txtAmount;
	private JTextField txtTotalPRice;
	private JTable productTable;
	private SalesOrderCtr soCtr;
	private JTable partOrderTable;
	private SalesOrder salesOrder;
	private PartOrderTableModel partOrderTableModel;
	private JScrollPane partOrderScrollPane;
	

	public SalesOrderUI() {
		customerCtr = new CustomerCtr();
		soCtr = SalesOrderCtr.getInstance();
		soCtr.createSalesOrder();
		setLayout(null);

		JPanel panelEdit = new JPanel();
		panelEdit.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Customer", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelEdit.setBounds(10, 10, 346, 250);
		add(panelEdit);
		panelEdit.setLayout(null);

		JLabel lblName = new JLabel("Name");
		lblName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblName.setBounds(10, 151, 85, 14);
		panelEdit.add(lblName);

		panelErrorCustomerName = new JPanel();
		panelErrorCustomerName.setForeground(Color.WHITE);
		panelErrorCustomerName.setBorder(new LineBorder(Color.LIGHT_GRAY, 3));
		panelErrorCustomerName.setBounds(104, 148, 134, 22);
		panelEdit.add(panelErrorCustomerName);
		panelErrorCustomerName.setLayout(null);

		txtName = new JTextField();
		txtName.setEditable(false);
		txtName.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				 
			}
		});
		txtName.setBounds(1, 1, 132, 20);
		panelErrorCustomerName.add(txtName);
		txtName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				 
			}
		});
		txtName.setColumns(10);

		JLabel lblCustomerAddress = new JLabel("Address");
		lblCustomerAddress.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCustomerAddress.setBounds(10, 52, 85, 14);
		panelEdit.add(lblCustomerAddress);

		panelErrorAddress = new JPanel();
		panelErrorAddress.setBorder(new LineBorder(Color.LIGHT_GRAY, 3));
		panelErrorAddress.setBounds(104, 46, 134, 22);
		panelEdit.add(panelErrorAddress);
		panelErrorAddress.setLayout(null);

		txtAddress = new JTextField();
		txtAddress.setEditable(false);
		txtAddress.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				 
			}
		});
		txtAddress.setBounds(1, 1, 132, 20);
		panelErrorAddress.add(txtAddress);
		txtAddress.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				 
				 
			}
		});
		txtAddress.setColumns(10);

		JLabel lblCodeZip = new JLabel("Codezip");
		lblCodeZip.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCodeZip.setBounds(10, 85, 85, 14);
		panelEdit.add(lblCodeZip);

		panelErrorCodeZip = new JPanel();
		panelErrorCodeZip.setBorder(new LineBorder(Color.LIGHT_GRAY, 3));
		panelErrorCodeZip.setBounds(104, 80, 134, 22);
		panelEdit.add(panelErrorCodeZip);
		panelErrorCodeZip.setLayout(null);

		txtCodeZip = new JTextField();
		txtCodeZip.setEditable(false);
		txtCodeZip.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
					 
			}
		});
		txtCodeZip.setBounds(1, 1, 132, 20);
		panelErrorCodeZip.add(txtCodeZip);
		txtCodeZip.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				 
			}
		});
		txtCodeZip.setColumns(10);

		JLabel lblCity = new JLabel("City");
		lblCity.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCity.setBounds(10, 118, 85, 14);
		panelEdit.add(lblCity);

		panelErrorCity = new JPanel();
		panelErrorCity.setForeground(Color.WHITE);
		panelErrorCity.setBorder(new LineBorder(Color.LIGHT_GRAY, 3));
		panelErrorCity.setBounds(104, 114, 134, 22);
		panelEdit.add(panelErrorCity);
		panelErrorCity.setLayout(null);

		txtCity = new JTextField();
		txtCity.setEditable(false);
		txtCity.setBounds(1, 1, 132, 20);
		panelErrorCity.add(txtCity);
		txtCity.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				 
				 
			}
		});
		txtCity.setColumns(10);

		JLabel lblEmail = new JLabel("Email");
		lblEmail.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEmail.setBounds(10, 184, 85, 14);
		panelEdit.add(lblEmail);

		panelErrorEmail = new JPanel();
		panelErrorEmail.setForeground(Color.WHITE);
		panelErrorEmail.setBorder(new LineBorder(Color.LIGHT_GRAY, 3));
		panelErrorEmail.setBounds(104, 182, 134, 22);
		panelEdit.add(panelErrorEmail);
		panelErrorEmail.setLayout(null);

		txtEmail = new JTextField();
		txtEmail.setEditable(false);
		txtEmail.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				 
			}
		});
		txtEmail.setBounds(1, 1, 132, 20);
		panelErrorEmail.add(txtEmail);
		txtEmail.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				 
			}
		});
		txtEmail.setColumns(10);

		JLabel lblCprCvr = new JLabel("Cpr / Cvr");
		lblCprCvr.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCprCvr.setBounds(10, 217, 85, 14);
		panelEdit.add(lblCprCvr);

		panelErrorCprCvr = new JPanel();
		panelErrorCprCvr.setForeground(Color.WHITE);
		panelErrorCprCvr.setBorder(new LineBorder(Color.LIGHT_GRAY, 3));
		panelErrorCprCvr.setBounds(104, 216, 134, 22);
		panelEdit.add(panelErrorCprCvr);
		panelErrorCprCvr.setLayout(null);

		txtCprCvr = new JTextField();
		txtCprCvr.setEditable(false);
		txtCprCvr.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				 
			}
		});
		txtCprCvr.setBounds(1, 1, 132, 20);
		panelErrorCprCvr.add(txtCprCvr);
		txtCprCvr.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				 
			}
		});
		txtCprCvr.setColumns(10);

		JLabel labelPhoneNo = new JLabel("PhoneNo");
		labelPhoneNo.setHorizontalAlignment(SwingConstants.RIGHT);
		labelPhoneNo.setBounds(10, 19, 85, 14);
		panelEdit.add(labelPhoneNo);

		panelErrorPhoneNo = new JPanel();
		panelErrorPhoneNo.setLayout(null);
		panelErrorPhoneNo.setForeground(Color.WHITE);
		panelErrorPhoneNo.setBorder(new LineBorder(Color.LIGHT_GRAY, 3));
		panelErrorPhoneNo.setBounds(104, 12, 134, 22);
		panelEdit.add(panelErrorPhoneNo);

		txtPhoneNo = new JTextField();
		txtPhoneNo.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				 
			}
		});
		txtPhoneNo.setColumns(10);
		txtPhoneNo.setBounds(1, 1, 132, 20);
		txtPhoneNo.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				 
			}
		});
		panelErrorPhoneNo.add(txtPhoneNo);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchCustomer();
			}
		});
		btnSearch.setBounds(248, 13, 89, 23);
		panelEdit.add(btnSearch);
		
		btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnClear.setBounds(248, 47, 89, 23);
		panelEdit.add(btnClear);
		customerTableModel = new CustomerTableModel();
		customerTableModel.PopulateList(customerCtr.getAllCustomers());
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Date", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 271, 346, 151);
		add(panel);
		panel.setLayout(null);
		
		JLabel lblDate = new JLabel("Current date");
		lblDate.setBounds(10, 29, 85, 14);
		lblDate.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblDate);
		
		txtDate = new JTextField();
		txtDate.setEditable(false);
		txtDate.setBounds(105, 26, 132, 20);
		txtDate.setText(dateFormat.format(date));
		panel.add(txtDate);
		txtDate.setColumns(10);
		
		JLabel lblDeliveryDate = new JLabel("Delivery date");
		lblDeliveryDate.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDeliveryDate.setBounds(10, 68, 85, 14);
		panel.add(lblDeliveryDate);
		
		textField = new JTextField();
		textField.setText("2013/04/07");
		textField.setColumns(10);
		textField.setBounds(105, 65, 132, 20);
		panel.add(textField);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Payment options", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 433, 346, 126);
		add(panel_1);
		panel_1.setLayout(null);
		
		JRadioButton rdbtnCOD = new JRadioButton("Cash on delivery");
		buttonGroupPayment.add(rdbtnCOD);
		rdbtnCOD.setSelected(true);
		rdbtnCOD.setBounds(19, 27, 179, 23);
		panel_1.add(rdbtnCOD);
		
		JRadioButton rdbtnDebit = new JRadioButton("Debit");
		buttonGroupPayment.add(rdbtnDebit);
		rdbtnDebit.setBounds(19, 53, 179, 23);
		panel_1.add(rdbtnDebit);
		
		JRadioButton rdbtnCheckout = new JRadioButton("Checkout");
		buttonGroupPayment.add(rdbtnCheckout);
		rdbtnCheckout.setBounds(19, 83, 179, 23);
		panel_1.add(rdbtnCheckout);
		
		JPanel panelTableModel = new JPanel();
		panelTableModel.setLayout(null);
		panelTableModel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Product list", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelTableModel.setBounds(382, 10, 612, 250);
		add(panelTableModel);
		
		JScrollPane SalesProductScrollPane = new JScrollPane();
		SalesProductScrollPane.setBounds(12, 13, 590, 189);
		productTable = new JTable();
		prodTableModel = new SalesProductTableModel();
		prodTableModel.populateList();
		productTable.setModel(prodTableModel);
		SalesProductScrollPane.setViewportView(productTable);
		panelTableModel.add(SalesProductScrollPane);
		
		JLabel label = new JLabel("Filter:");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setBounds(12, 218, 46, 14);
		panelTableModel.add(label);
		
		txtFilter = new JTextField();
		txtFilter.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				filterProducts();
			}
		});
		txtFilter.setColumns(10);
		txtFilter.setBounds(68, 214, 116, 22);
		panelTableModel.add(txtFilter);
		
		JButton btnAddPartOrder = new JButton("Add");
		btnAddPartOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddPartOrder(productTable.getSelectedRow());
			}
		});
		btnAddPartOrder.setBounds(505, 213, 97, 25);
		panelTableModel.add(btnAddPartOrder);
		
		txtAmount = new JTextField();
		txtAmount.setBounds(394, 215, 86, 20);
		panelTableModel.add(txtAmount);
		txtAmount.setColumns(10);
		
		JLabel lblAmount = new JLabel("amount");
		lblAmount.setBounds(338, 218, 46, 14);
		panelTableModel.add(lblAmount);
		
		JPanel panel_2 = new JPanel();
		panel_2.setLayout(null);
		panel_2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Current order", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(382, 271, 612, 288);
		add(panel_2);
		
		partOrderScrollPane = new JScrollPane();
		partOrderScrollPane.setBounds(12, 13, 590, 189);
		panel_2.add(partOrderScrollPane);
		
		partOrderTable = new JTable();
		partOrderTableModel = new PartOrderTableModel();
		//partOrderTableModel.populateList();
		partOrderTable.setModel(partOrderTableModel);
		partOrderScrollPane.setViewportView(partOrderTable);
		
		
		JButton btnCreateOrder = new JButton("Create order");
		btnCreateOrder.setBounds(490, 252, 112, 25);
		panel_2.add(btnCreateOrder);
		
		txtTotalPRice = new JTextField();
		txtTotalPRice.setColumns(10);
		txtTotalPRice.setBounds(490, 213, 112, 20);
		panel_2.add(txtTotalPRice);
		
		JLabel lblTotalPrice = new JLabel("Total price");
		lblTotalPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTotalPrice.setBounds(368, 213, 112, 14);
		panel_2.add(lblTotalPrice);
		
		JButton btnClearData = new JButton("Clear data");
		btnClearData.setBounds(368, 253, 112, 25);
		panel_2.add(btnClearData);
		
		JButton btnRemoveProduct = new JButton("Remove product");
		btnRemoveProduct.setBounds(12, 211, 112, 25);
		panel_2.add(btnRemoveProduct);
		
	}

	/**
	 * Clear inputs, all are empty.
	 */
	protected void clearInputs() {
		txtName.setText("");
		txtAddress.setText("");
		txtCodeZip.setText("");
		txtCity.setText("");
		txtPhoneNo.setText("");
		txtEmail.setText("");
		txtCprCvr.setText("");
	}

	private void searchCustomer ()
	{
		Customer c = customerCtr.getCustomerByPhoneNo(Integer.parseInt(txtPhoneNo.getText()));
		System.out.print(c.getName());
			txtName.setText(c.getName());
			txtAddress.setText(c.getAddress());
			txtCodeZip.setText(""+c.getCodeZip());
			txtCity.setText(c.getCity());
			txtEmail.setText(c.getEmail());
			txtCprCvr.setText(c.getCprCvr());			
	}

	protected void filterProducts() {
		prodTableModel.populateList(txtFilter.getText());
	}
	
	private void AddPartOrder(int selectedRow)
	{
		if(selectedRow > -1)
		{
			ProductCtr prodCtr = new ProductCtr();
			Product prod = prodCtr.findProd((int) productTable.getModel().getValueAt(selectedRow, 0));
			soCtr.createPartOrder(prod, Integer.parseInt(txtAmount.getText()));
			putValueOnScreen();
		}
	}
	
	private void putValueOnScreen()
	{
		partOrderTableModel.populateList();
		partOrderTable.setModel(partOrderTableModel);
		partOrderScrollPane.setViewportView(partOrderTable);
		System.out.println("partorders inside order: "+partOrderTableModel.getRowCount());
	}
}
	